﻿/*
 * Created by: 
 * Created: Tuesday, April 24, 2007
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Dune.Common.ControlFlow;
using Dune.Common.Utils;
using Iesi.Collections;

namespace Dune.Common.TextTemplates.Impl.ASPNET
{
    public class TextEngineFactory
    {
        private Dictionary<string, TextEngine> pathsEngines = new Dictionary<string, TextEngine>();

        private readonly List<Assembly> referensec = new List<Assembly>();

        #region Singletone implementation

        private static TextEngineFactory _instance;

        public static TextEngineFactory Instance()
        {
            if (_instance == null)
            {
                lock (typeof(TextEngineFactory))
                {
                    if (_instance == null)
                    {
                        _instance = new TextEngineFactory();
                    }
                }
            }

            return _instance;
        }

        #endregion

        public List<Assembly> Referensec
        {
            get { return referensec; }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="path"></param>
        /// <param name="needReloading"> true for design mode, false for runtime mode</param>
        /// <returns></returns>
        public TextEngine GetEngine(string path, bool needReloading)
        {
            if (path == null)
                throw new ArgumentException("path");

            if (!pathsEngines.ContainsKey(path))
            {
                lock (this)
                {
                    if (!pathsEngines.ContainsKey(path))
                    {
                        TextEngine engine = new TextEngine();

                        if (!needReloading)
                        {
                            //  reate temp directory for temlates web app
                            string sysTempPath = Path.GetTempPath();
                            Assert.Check(!StringUtils.IsBlankOrNull(sysTempPath));
                            if (!sysTempPath.EndsWith(Path.DirectorySeparatorChar.ToString()))
                                sysTempPath += Path.DirectorySeparatorChar;
                            string tempWebAppPath = sysTempPath + Guid.NewGuid();

                            // copy files to temp web app directory
                            FileUtils.CoptDirectory(path, tempWebAppPath);

                            engine.TemplatesDir = tempWebAppPath;
                        }
                        else
                            engine.TemplatesDir = path;

                        engine.NeedReloading = needReloading;
                        engine.References = referensec.ToArray();
                        engine.Init();
                        pathsEngines[path] = engine;
                        return engine;
                    }
                }
            }

            return pathsEngines[path];
        }

        public void Deitialize()
        {
            // TODO clear temp directories
        }
    }
}